/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxProduct = function (root) {
  let treeSum = 0
  let res = 0
  treeSum = getSum(root) // 得到这个树的和
  getSum(root) // 重新遍历一次得到这个最大的some
  function getSum (root) {
    if (root === null) return 0
    let leftSum = getSum(root.left)
    let rightSum = getSum(root.right)
    let sum = leftSum + rightSum + root.val
    res = Math.max(res, (treeSum - sum) * sum)
    return sum
  }
  return res % (1e9 + 7)
};